const path = require("path")
const swaggerUI = require("swagger-ui-express")
const swaggerDoc = require("swagger-jsdoc")

const options = {
    definition:{
        openapi:"3.0.0",
        info:{
            title:'接口文档',
            version:"1.0.0",
            description:'hcj笔记记录系统 api'
        }
    },
    //去哪个目录下收集swagger注释
    apis:[path.join(__dirname,'../router/routes/*.js')]
}

var swaggerJson = function(req,res){
    res.setHeader('Content-Type','application/json')
    res.send(swaggerSpec)
}

const swaggerSpec = swaggerDoc(options)

var swaggerInstall = function(app){
    if(!app){
        app = express()
    }
    app.get("/swagger.json",swaggerJson)
    app.use("/swagger",swaggerUI.serve,swaggerUI.setup(swaggerSpec))
}

module.exports = swaggerInstall